package com.ruin.app.controller;

import com.alibaba.fastjson.JSON;
import com.ruin.app.util.ElasticUtil;
import com.ruin.app.util.Render;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.net.UnknownHostException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/es")
public class ElasticSearchController {

    /**
     * 获取索引数据
     *
     * @param index
     * @param type
     * @param id
     * @return
     */
    @GetMapping(value = "/index/get/{index}/{type}/{id}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object getIndex(@PathVariable String index, @PathVariable String type, @PathVariable String id) {
        try {
            return Render.success(ElasticUtil.getIndex(index, type, id));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return Render.error();
    }

    /**
     * 查询索引数据
     *
     * @param index
     * @param type
     * @return
     */
    @GetMapping(value = "/index/search/{index}/{type}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object searchIndex(@PathVariable String index, @PathVariable String type) {
        try {
            List<Map<String, Object>> datas = ElasticUtil.searchIndex(index, type);
            return Render.success(datas);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return Render.error();
    }

    @PostMapping(value = "/index/query/{index}/{type}", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object searchData(@PathVariable String index, @PathVariable String type, String param) {
        try {
            if (!StringUtils.isEmpty(param)) {
                String name = param.split("=")[0];
                String keyWord = param.split("=")[1];
                List<Map<String, Object>> datas = ElasticUtil.searchData(index, type, name, keyWord);
                return Render.success(datas);
            } else {
                return Render.success("无效参数");
            }

        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return Render.error();
    }

    /**
     * 更新索引值
     *
     * @param index
     * @param type
     * @param id
     * @param json
     * @return
     */
    @PostMapping(value = "/index/update/{index}/{type}/{id}")
    public Object updateIndex(@PathVariable String index, @PathVariable String type, @PathVariable String id, String json) {

        Map map = (Map) JSON.parse(json);
        try {
            return Render.success(ElasticUtil.updateIndex(index, type, id, map));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return Render.error();
    }

    @PostMapping(value = "/index/add/{index}/{type}/{id}")
    public Object addIndex(String index, String type, String id, String json) {
        Map map = (Map) JSON.parse(json);
        try {
            return Render.success(ElasticUtil.addIndex(index, type, id, map));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return Render.error();
    }
}
